package com.lframework.xingyun.basedata.impl.member;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lframework.starter.web.core.annotations.oplog.OpLog;
import com.lframework.starter.web.core.impl.BaseMpServiceImpl;
import com.lframework.starter.web.core.utils.OpLogUtil;
import com.lframework.starter.web.core.utils.PageResultUtil;
import com.lframework.starter.web.core.components.resp.PageResult;
import com.lframework.starter.common.utils.Assert;
import com.lframework.xingyun.basedata.bo.member.QueryMemberAccountLogBo;
import com.lframework.xingyun.basedata.entity.MemberAccountLog;
import com.lframework.xingyun.basedata.enums.BaseDataOpLogType;
import com.lframework.xingyun.basedata.mappers.MemberAccountLogMapper;
import com.lframework.xingyun.basedata.service.member.MemberAccountLogService;
import com.lframework.xingyun.basedata.vo.member.CreateMemberAccountLogVo;
import com.lframework.xingyun.basedata.vo.member.QueryMemberAccountLogVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;

import java.util.stream.Collectors;

@Service
public class MemberAccountLogServiceImpl extends BaseMpServiceImpl<MemberAccountLogMapper, MemberAccountLog> implements MemberAccountLogService {
    /**
     * 查询会员积分账户变动记录
     * @param vo        查询参数
     * @return 会员积分账户变动记录列表
     */
    @Override
    public PageResult<QueryMemberAccountLogBo> query(Integer pageIndex, Integer pageSize, QueryMemberAccountLogVo vo) {

        Assert.greaterThanZero(pageIndex);
        Assert.greaterThanZero(pageSize);

        IPage<MemberAccountLog> pageData = this.baseMapper.query(pageIndex, pageSize, vo);

        IPage<QueryMemberAccountLogBo> page = new Page<>(pageData.getCurrent(), pageData.getSize(), pageData.getTotal());
        page.setRecords(pageData.getRecords().stream().map(QueryMemberAccountLogBo::new).collect(Collectors.toList()));

        return PageResultUtil.convert(page);
    }

    @OpLog(type = BaseDataOpLogType.class, name = "会员积分账户变动记录，ID：{}, 会员id：{}, 变动金额：{}",
            params = {"#id", "#memberId", "#changeScore"})
    @Transactional(rollbackFor = Exception.class)
    @Override
    public String add(@Validated CreateMemberAccountLogVo vo) {

        MemberAccountLog entity = new MemberAccountLog();
        entity.setMemberId(vo.getMemberId());
        entity.setChangeType(vo.getChangeType());
        entity.setBeforeScore(vo.getBeforeScore());
        entity.setChangeScore(vo.getChangeScore());
        entity.setAfterScore(vo.getAfterScore());
        entity.setReason(vo.getReason());
        this.baseMapper.insert(entity);

        OpLogUtil.setVariable("id", entity.getId());
        OpLogUtil.setVariable("memberId", entity.getMemberId());
        OpLogUtil.setVariable("changeScore", entity.getChangeScore());

        return entity.getId();
    }


}

